package com.yedean.mall.token;

import cn.hutool.core.codec.Base64;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * TokenUtilMd5Generator
 *
 * @author yedean
 * @date 2022/8/17
 */
public class TokenUtilMd5Generator implements TokenGenerator{
    
    private static final ThreadLocal<MessageDigest> MD5_LOCAL=ThreadLocal.withInitial(()->{
        try {
            return MessageDigest.getInstance("MD5");
        }catch (NoSuchAlgorithmException e){
            throw new RuntimeException("未找到MD5算法", e);
        }
    });
    @Override
    public String generator() {
        String uuid = UUID.randomUUID().toString();
        MessageDigest digest = MD5_LOCAL.get();
        digest.reset();
        byte[] bytes = digest.digest(uuid.getBytes(StandardCharsets.UTF_8));
        return Base64.encode(bytes);
    }
}
